using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Security.Cryptography;


namespace GMAO.CORE
{
    public class GMAO_USER
    {
        #region Declarations
        private string _ID = "-1";
        private string _Nom = "";
        private string _Prenom = "";
        private string _Login = "";
        private string _MDP = "";
        private string _Last_IP = "";
        private string _Date_Creation = "";
        private string _Date_Last_Login = "";
        private bool _isActif = false;

        #endregion

        public bool IsActif
        {
            get { return _isActif; }
            set { _isActif = value; }
        }

        public string ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        
        public string Nom
        {
            get { return _Nom; }
            set { _Nom = value; }
        }
        
        public string Prenom
        {
            get { return _Prenom; }
            set { _Prenom = value; }
        }
        
        public string Login
        {
            get { return _Login; }
            set { _Login = value; }
        }
        
        public string MDP
        {
            get { return _MDP; }
            set { _MDP = value; }
        }
        
        public string Last_IP
        {
            get { return _Last_IP; }
            set { _Last_IP = value; }
        }
        
        public string Date_Creation
        {
            get { return _Date_Creation; }
            set { _Date_Creation = value; }
        }

        public string Date_Last_Login
        {
            get { return _Date_Last_Login; }
            set { _Date_Last_Login = value; }
        }

        public string CreateUser()
        {
            StringBuilder sRequete = new StringBuilder();
            sRequete.AppendFormat(@"
            INSERT
            INTO   [UTILISATEUR]
                   (
                           NOM            ,
                           PRENOM         ,
                           LOGIN          ,
                           MDP            ,
                           LAST_IP        ,
                           DATE_LAST_LOGIN,
                           DATE_CRATION   ,
                           ACTIF
                   )
                   VALUES
                   (
                           '{0}' ,
                           '{1}' ,
                           '{2}' ,
                           '{3}' ,
                           '{4}' ,
                           '{5}' ,
                           '{6}'
                   )

            ", _Nom, _Prenom, _Login,Fonctions.MD5Crypt(_MDP), _Last_IP, _Date_Creation);
            _ID = DataProviderFactoy.GetInstance().ExecuteNonQuery(sRequete.ToString()).ToString();
            return _ID;
        }

        public static bool AuthenticateUser(string login, string MDP)
        {
            string MDP_STORED = "";
            StringBuilder sRequete = new StringBuilder();
            sRequete.AppendFormat(@"
                SELECT LOGIN,
                       MDP
                FROM   UTILISATEUR
                WHERE
                       (
                              LOGIN = '{0}'
                       )
                    AND
                       (
                              ACTIF = '1'
                       )
            ", login);
            DataRowCollection DR;
            DR = DataProviderFactoy.GetInstance().ExecuteGetDataRow(sRequete.ToString());
            if (DR.Count > 0)
            {
                MDP_STORED = DR[0]["MDP"].ToString();
                if (MDP_STORED == Fonctions.MD5Crypt(MDP))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        public bool setActif()
        {
            int AffectedRows = 0;
            StringBuilder sRequete = new StringBuilder();
            sRequete.Append(@"
            UPDATE UTILISATEUR
            SET    
            ");
            sRequete.AppendFormat(@"
                   ACTIF           = '{0}'
            ", _isActif == true ? "1":"0");
            sRequete.AppendFormat(@"
            WHERE
                   (
                          ID = '{0}'
                   )
            ", _ID);
            AffectedRows = int.Parse(DataProviderFactoy.GetInstance().ExecuteNonQuery(sRequete.ToString()).ToString());
            if (AffectedRows == 1)
                return true;
            else
                return false;
        }

        public bool changeMDP()
        {
            int AffectedRows = 0;
            StringBuilder sRequete = new StringBuilder();
            sRequete.Append(@"
            UPDATE UTILISATEUR
            SET    
            ");
            sRequete.AppendFormat(@"
                   MDP             = '{0}',
            ", Fonctions.MD5Crypt(_MDP));
            sRequete.AppendFormat(@"
            WHERE
                   (
                          ID = '{0}'
                   )
            ", _ID);
            AffectedRows = int.Parse(DataProviderFactoy.GetInstance().ExecuteNonQuery(sRequete.ToString()).ToString());
            if (AffectedRows == 1)
                return true;
            else
                return false;
        }

        public bool updateUser()
        {
            int AffectedRows = 0;
            StringBuilder sRequete = new StringBuilder();
            sRequete.Append(@"
            UPDATE UTILISATEUR
            SET    
            ");
            if (_Nom != "")
            {
                sRequete.AppendFormat(@"
                   NOM             = '{0}',
                ", _Nom);
            }
            if (_Login != "")
            {
                sRequete.AppendFormat(@"
                   LOGIN           = '{0}',
                ", _Login);
            }
            if (_Prenom != "")
            {
            sRequete.AppendFormat(@"
                   PRENOM          = '{0}',
            ", _Prenom);
            }
            if (_Last_IP != "")
            {
            sRequete.AppendFormat(@"
                   LAST_IP         = '{0}',
            ", _Last_IP);
            }
            if (_Date_Last_Login != "")
            {
            sRequete.AppendFormat(@"
                   DATE_LAST_LOGIN = '{0}',
            ", _Date_Last_Login);
            }
            sRequete.AppendFormat(@"
            WHERE
                   (
                          ID = '{0}'
                   )
            ", _ID);
            AffectedRows = int.Parse(DataProviderFactoy.GetInstance().ExecuteNonQuery(sRequete.ToString()).ToString());
            if (AffectedRows == 1)
                return true;
            else
                return false;
        }

        public static bool ValidatePassword(string password)
		{
			// Very simple password rule. Extend here when required.
			return (password.Length >= 5);
		}
        
        public void LoadUserByID()
        { 
            StringBuilder sRequete = new StringBuilder();
            sRequete.AppendFormat(@"
            SELECT 
                   ID             ,
                   NOM            ,
                   PRENOM         ,
                   LOGIN          ,
                   MDP            ,
                   LAST_IP        ,
                   DATE_LAST_LOGIN,
                   DATE_CRATION   ,
                   ACTIF
            FROM   [UTILISATEUR]
            WHERE
                   (
                          ID = {0}
                   )
            ", _ID);
            DataRowCollection DR;
            DR = DataProviderFactoy.GetInstance().ExecuteGetDataRow(sRequete.ToString());
            _ID = DR[0]["ID"].ToString();
            _Nom = DR[0]["NOM"].ToString();
            _Prenom = DR[0]["PRENOM"].ToString();
            _Login = DR[0]["LOGIN"].ToString();
            _MDP = DR[0]["MDP"].ToString();
            _Last_IP = DR[0]["LAST_IP"].ToString();
            _Date_Creation = DR[0]["DATE_CRATION"].ToString();
            _isActif = DR[0]["ACTIF"].ToString().Trim() == "0" ? false : true;
        }
        public void LoadUserByLOGIN()
        {
            StringBuilder sRequete = new StringBuilder();
            sRequete.AppendFormat(@"
            SELECT 
                   ID             ,
                   NOM            ,
                   PRENOM         ,
                   LOGIN          ,
                   MDP            ,
                   LAST_IP        ,
                   DATE_LAST_LOGIN,
                   DATE_CRATION   ,
                   ACTIF
            FROM   [UTILISATEUR]
            WHERE
                   (
                          LOGIN = '{0}'
                   )
            ", _Login);
            DataRowCollection DR;
            DR = DataProviderFactoy.GetInstance().ExecuteGetDataRow(sRequete.ToString());
            _ID = DR[0]["ID"].ToString();
            _Nom = DR[0]["NOM"].ToString();
            _Prenom = DR[0]["PRENOM"].ToString();
            _Login = DR[0]["LOGIN"].ToString();
            _MDP = DR[0]["MDP"].ToString();
            _Last_IP = DR[0]["LAST_IP"].ToString();
            _Date_Creation = DR[0]["DATE_CRATION"].ToString();
            _isActif = DR[0]["ACTIF"].ToString().Trim() == "0" ? false : true;
        }
    }
}
